package client;

import java.rmi.RemoteException;

public class Client 
{
	public static void main(String[] args) throws RemoteException 
	{
	    SalesAutomationServiceStub.Product sasp = 
			SalesAutomationService_SelectProduct(1);
			
		DatabaseServiceStub.Product dbsp = new DatabaseServiceStub.Product();
		dbsp.setProductId(sasp.getProductId());
		dbsp.setDescription(sasp.getDescription());
		dbsp.setValue(sasp.getValue());
		
		if (DatabaseService_ConsultInventory(dbsp))
		{
			SalesAutomationServiceStub.Client sasc = 
					new SalesAutomationServiceStub.Client();
			sasc.setClientId(1);
			sasc.setName("diego");
			sasc.setCreditNumber("123");
			
			if (SalesAutomationService_MakePurchase(sasp, sasc))
			{
				System.out.println("Purchase made successfully.");
				
				CreditCardServiceStub.Client ccsc =
						new CreditCardServiceStub.Client();
				ccsc.setClientId(sasc.getClientId());
				ccsc.setName(sasc.getName());
				ccsc.setCreditNumber(sasc.getCreditNumber());
				
				if (CreditCardService_MakePayment(ccsc, sasp.getValue()))
				{
					System.out.println("Payment made successfully.");
					
					StorageServiceStub.Client ssc =
							new StorageServiceStub.Client();
					ssc.setClientId(sasc.getClientId());
					ssc.setName(sasc.getName());
					ssc.setCreditNumber(sasc.getCreditNumber());
					
					StorageServiceStub.Product ssp =
							new StorageServiceStub.Product();
					ssp.setProductId(sasp.getProductId());
					ssp.setDescription(sasp.getDescription());
					ssp.setValue(sasp.getValue());
					
					if (StorageService_StoreFileCloud(ssc, ssp))
					{
						System.out.println("Receipt stored successfully.");
						
						if (DatabaseService_UpdateInventory(dbsp))
						{
							System.out.println("Inventory updated successfully");
							
							/*MessagingServiceStub.Client msgc =
									new MessagingServiceStub.Client();
							msgc.setClientId(sasc.getClientId());
							msgc.setName(sasc.getName());
							msgc.setCreditNumber(sasc.getCreditNumber());
							
							MessagingServiceStub.Product msgp =
									new MessagingServiceStub.Product();
							msgp.setProductId(sasp.getProductId());
							msgp.setDescription(sasp.getDescription());
							msgp.setValue(sasp.getValue());
							
							System.out.println(MessagingService_SendMessage(msgc, msgp));*/
						}
						else
							System.out.println("Error during inventory update.");
					}
					else
						System.out.println("Error during recipe storage.");
				}
				else
					System.out.println("You purchase could not be performed.\nYou do not have sufficient balance to make this purchase.");
			}
			else
				System.out.println("Error during purchasing operation.");
		}
		else
			System.out.println("The selected product is unavailable.");
	}


	private static SalesAutomationServiceStub.Product SalesAutomationService_SelectProduct(int i) 
		throws RemoteException 
	{
		// Stub
		SalesAutomationServiceStub stub = new SalesAutomationServiceStub("http://23.21.56.91:8080/axis2/services/SalesAutomationService.SalesAutomationServiceHttpSoap12Endpoint");
		
		// Service invocation
		SalesAutomationServiceStub.Product response = stub.selectProduct(i);
		
		// Response
		return response;
	}
	
	
	private static boolean DatabaseService_ConsultInventory(DatabaseServiceStub.Product product) 
		throws RemoteException 
	{
		// Stub
		DatabaseServiceStub stub = new DatabaseServiceStub("http://23.21.56.91:8080/axis2/services/DatabaseService.DatabaseServiceHttpSoap12Endpoint");
		
		// Service invocation
		boolean response = stub.consultInventory(product);
		
		// Response
		return response;
	}
	
	
	private static boolean SalesAutomationService_MakePurchase(SalesAutomationServiceStub.Product product, 
		SalesAutomationServiceStub.Client client) throws RemoteException 
	{
		// Stub
		SalesAutomationServiceStub stub = new SalesAutomationServiceStub("http://23.21.56.91:8080/axis2/services/SalesAutomationService.SalesAutomationServiceHttpSoap12Endpoint");
		
		// Service invocation
		boolean response = stub.makePurchase(product, client);
		
		// Response
		return response;
	}
	
	
	private static boolean CreditCardService_MakePayment(CreditCardServiceStub.Client client, double value) 
		throws RemoteException 
	{
		// Stub
		CreditCardServiceStub stub = new CreditCardServiceStub("http://23.21.56.91:8080/axis2/services/CreditCardService.CreditCardServiceHttpSoap12Endpoint");
		
		// Service invocation
		boolean response = stub.makePayment(client, value);
		
		// Response
		return response;
	}
	
	
	private static boolean StorageService_StoreDatabase(StorageServiceStub.Client client, StorageServiceStub.Product product) 
			throws RemoteException 
	{
		// Stub
		StorageServiceStub stub = new StorageServiceStub();
			
		// Service invocation
		boolean response = stub.storeDatabase(client, product);
			
		// Response
		return response;
	}
	
	
	private static boolean StorageService_StoreFileLocal(StorageServiceStub.Client client, StorageServiceStub.Product product) 
			throws RemoteException 
	{
		// Stub
		StorageServiceStub stub = new StorageServiceStub();
			
		// Service invocation
		boolean response = stub.storeFileLocal(client, product);
			
		// Response
		return response;
	}
	
	
	private static boolean StorageService_StoreFileCloud(StorageServiceStub.Client client, StorageServiceStub.Product product) 
		throws RemoteException 
	{
		// Stub
		StorageServiceStub stub = new StorageServiceStub("http://23.21.56.91:8080/axis2/services/StorageService.StorageServiceHttpSoap12Endpoint");
			
		// Service invocation
		boolean response = stub.storeFileCloud(client, product);
		
		// Response
		return response;
	}
	
	
	private static boolean DatabaseService_UpdateInventory(DatabaseServiceStub.Product product) 
		throws RemoteException 
	{
		// Stub
		DatabaseServiceStub stub = new DatabaseServiceStub("http://23.21.56.91:8080/axis2/services/DatabaseService.DatabaseServiceHttpSoap12Endpoint");
		
		// Service invocation
		boolean response = stub.updateInventory(product);
		
		// Response
		return response;
	}
	
	
	private static String MessagingService_SendMessage(MessagingServiceStub.Client client, MessagingServiceStub.Product product) 
		throws RemoteException 
	{
		// Stub
		MessagingServiceStub stub = new MessagingServiceStub();
		
		// Service invocation
		String response = stub.sendMessage(client, product);
		
		// Response
		return response;
	}
}